<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomePayController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    // ======================================================充值显示
    public function getIndex()
    {
       

        return view('Home/pay/pay');
    }
    // ========================================================执行充值
    public function postInsert(Request $request)
    {
        $res = $request->only('username','number','password');
        $card = DB::table('dota_pay')->where('number',$res['number'])->first();
        $cards = DB::table('dota_pay')->where('password',$res['password'])->first();
        if(!$request->input('username')){
            return back()->with('error','用户名未填写 ');
        }
        if(!$request->input('number')){
            return back()->with('error','一卡通账号未填写 ');
        }
        $user = $res['username'];
        
        $users = DB::table('dota_user')->where('email',$user)->first();

        if(empty($users)){
            return back()->with('error','该用户不存在');
        }    
        if($card && $cards){
            $ba = DB::table('dota_user')->where('email',$user)->first();
            $id = $ba->id;
             if($card->status == 2){
            return back()->with('error','一卡通已过期');
            }else{
        

            //生成充值订单
            $max = DB::table('dota_orders')->max('pay_id');
            if(!empty($max)){
            $iid = $max + 1;
            }else{
                $iid = 100000;
            }
            $jine = $card->amount;
            $uid = $users->id;
            
            $time = time();
            
            $dd = DB::table('dota_orders')->insert(['pay_id'=>$iid,'sum'=>$jine,'time'=>$time,'state'=>1,'uid'=>$uid]);

            $amount = $card->amount += $ba->balance;
            $balance = DB::table('dota_user')->where('id',$id)->update(['balance'=>$amount]);  
            $up = DB::table('dota_pay')->where('id',$card->id)->update(['status'=> 2]);
            return back()->with('success','充值成功');  

            }


            // $orders = DB::table('')

                }else{
                    return back()->with('error','密码错误');
                }


            

         


        
    }

    public function getInserta(Request $request){
        $num = $request->num;
        $res = DB::table('dota_pay')->where('number',$num)->first();
        $amo = $res->amount;
        if($amo == 500){
        echo 0;
        }
        if($amo == 50){
            echo 1;
        }
        if($amo == 200){
            echo 2;
        }
    
    }
}
